package com.mmall.controller.backend;

import com.google.common.collect.Maps;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.service.IUserService;
import com.mmall.util.PropertiesUtil;
import com.mmall.vo.ProductDetailVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/7 0007.
 */

@Controller
@RequestMapping("/manage/product")
public class ProductManageController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IFileService iFileService;


    /**
     * 增加产品
     *
     * @param session
     * @param product
     * @return
     */
    @RequestMapping(value = "save.do" )
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登陆管理员账号");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充增加产品的业务逻辑
            return iProductService.saveOrUpdateProduct(product);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }

    }

    /**
     * 设置产品售卖状态
     *
     * @param session
     * @param productId
     * @param status
     * @return
     */
    @RequestMapping("set_sale_status.do")
    @ResponseBody
    public ServerResponse setSellStatus(HttpSession session, Integer productId, Integer status) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登陆管理员账号");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充设置产品售卖状态的业务逻辑
            return iProductService.setSellStatus(productId, status);
        }
        {
            return ServerResponse.createByErrorMessage("无权限操作");
        }

    }


    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse getDetail(HttpSession session, Integer productId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登陆管理员账号");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return iProductService.getManageProductDetail(productId);

        }
        {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse getList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登陆管理员账号");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充获取 产品列表 的业务逻辑
            return iProductService.getProductList(pageNum, pageSize);
        }
        {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse productSearch(HttpSession session, String productName, Integer productId, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登陆管理员账号");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充获取 搜索产品 的业务逻辑
            return iProductService.searchProduct(productName, productId, pageSize, pageNum);
        }
        {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

    /**
     * @param session
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("upload.do")
    @ResponseBody
    //------------------------------------------------MultipartFile:springMVC文件上传工具，在dispatcher-servlet.xml里进行配置。
    public ServerResponse upload(HttpSession session, @RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeAndMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，请登陆管理员账号");
        }
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充获取 上传文件 的业务逻辑
            //这个路径上传完之后，会创建到发布后的webapp里和WEB-INF  index.jsp是同一级的
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file, path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;

            Map fileMap = Maps.newHashMap();
            fileMap.put("uri", targetFileName);
            fileMap.put("url", url);
            return ServerResponse.createBySuccessData(fileMap);
        }
        {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }


    @RequestMapping("richText_img_upload.do")
    @ResponseBody
    //------------------------------------------------MultipartFile:springMVC文件上传工具，在dispatcher-servlet.xml里进行配置。
    public Map richTestImgUpload(HttpSession session, HttpServletResponse response,@RequestParam(value = "upload_file", required = false) MultipartFile file, HttpServletRequest request) {
        Map resultMap = Maps.newHashMap();
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            resultMap.put("", "");
            resultMap.put("", "用户未登录，请登陆管理员账号");
            return resultMap;
        }

        //富文本对自己的返回值有要求，项目中使用的是simditor,所以要按照simditor的要求进行返回。
        /*
            "success": true/false,
                "msg": "message",
                "file_path": "[real file path]"
        */
        if (iUserService.checkAdminRole(user).isSuccess()) {
            //填充获取 上传文件 的业务逻辑
            //这个路径上传完之后，会创建到发布后的webapp里和WEB-INF  index.jsp是同一级的
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = iFileService.upload(file, path);

            if (StringUtils.isBlank(targetFileName)) {
                resultMap.put("success", "false");
                resultMap.put("", "上传失败");
            }
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix") + targetFileName;
            resultMap.put("success", "true");
            resultMap.put("msg", "上传成功");
            resultMap.put("file_path", url);
            response.addHeader("Access-Control-allow-Headers","X-File-Name");
            return resultMap;
        } else {
            resultMap.put("success", "false");
            resultMap.put("", "请登陆管理员账号");
            return resultMap;
        }
    }




}
